
#define _XOPEN_SOURCE

#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h>
#include <unistd.h>
int main(){
  key_t key = ftok("./", 0X666);
  if(key < 0){
    perror("ftok\n");
    return 1;
  }

  int shmid = shmget(key, 4096, IPC_CREAT | IPC_EXCL | 0666);//创建全新的，未被使用的共享内存
  if(shmid < 0){
    perror("shmget\n");
    return 2;
  }

  char* mem = (char*)shmat(shmid, NULL, 0);//与共享内存建立关联

  const char* msg = "i am process A";
  strcpy(mem, msg);
  sleep(15);

  shmdt(mem);//与共享内存移除关联
  shmctl(shmid, IPC_RMID, NULL);//释放贡献内存
  return 0;
}
